<?php

namespace app\admin\controller;

use think\Db;
use think\response\Json;

Class Area extends Base
{
    public function areaEdit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_area")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $sqldata = array();
            $sqldata["name"]=$data["name"];
            $sqldata["time"]=time();
            if (empty($id)){
                $res = Db::table("tb_area")->insert($sqldata);
            }else{
                $res = Db::table("tb_area")->where("id",$id)->update($sqldata);
            }
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }
        return view('', ['row' => $row]);
    }

    public function areaList(){
        $name = $this->request->get('name');
        $where = array();

        if ($name){
            $where[] = ['name', 'like', '%'.$name.'%'];
        }
        $list = Db::table("tb_area")->where($where)->order("time desc ,status desc")->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    public function areaStatus(){
        $data = $this->request->post();
        $status = $data['status'] == 0 ? 1 : 0;
        $res = Db::table("tb_area")->where('id', $data['id'])->setField('status', $status);
        if ($res) {
            return ['status' => 'y', 'info' => '操作成功'];
        } else {
            return ['status' => 'n', 'info' => '操作失败'];
        }
    }



}